package com.yt.app.api.action;

import com.yt.app.api.action.base.BaseAction;
import com.yt.app.api.vo.BaseVo;
import com.yt.app.core.interfaces.UserCartService;
import com.yt.app.core.model.UserCart;
import com.yt.app.core.model.UserContact;
import com.yt.core.MsgBean;
import com.yt.core.dao.filter.DynamicSQLFilterImpl;
import com.yt.core.dao.filter.QueryFilter;
import com.yt.order.api.interfaces.ProductService;
import com.yt.order.api.model.Product;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@Api(tags = "03.商城")
@RequestMapping("/my/cart")
public class MyCartController extends BaseAction{
    @Autowired
    private UserCartService userCartService;
    @Autowired
    private ProductService productService;

    @ApiOperation(value="购物车列表", httpMethod = "GET")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "购物车信息", response = UserCart.class)})
    @RequestMapping("/list")
    public @ResponseBody  Object listProfile(
            @ApiParam("上一页最后一个ID") @RequestParam(name="lastId", required = false, defaultValue = "0") Long lastId,
            @ApiIgnore BaseVo vo,
            @ApiIgnore HttpServletRequest request){
        MsgBean msgBean = new MsgBean();
        Long uid = BaseVo.getUIDFromHeader(request);
        QueryFilter filter = new DynamicSQLFilterImpl();
        filter.setPagingBean(null);
        filter.addFilter("Q_uid_L_EQ", String.valueOf(uid));
        List<UserCart> productList = userCartService.find(filter);
        msgBean.put("lastId", -1);
        msgBean.put("products", productList);

        return msgBean.returnMsg();
    }

    @ApiOperation(value="添加购物车", notes = "添加购物车，修改故购物车数量", httpMethod = "POST")
    @RequestMapping("/add")
    public @ResponseBody  Object updateProfile(
            @ApiParam("商品ID") @RequestParam(name="productId", required = true) Long productId,
            @ApiParam("商品数量，包括新增，以及修改购物车中商品数量，都默认1") @RequestParam(name="productCout", required = true) Integer productCount,
            @ApiIgnore BaseVo vo,
            @ApiIgnore HttpServletRequest request){
        MsgBean msgBean = new MsgBean();
        Long uid = BaseVo.getUIDFromHeader(request);
        Product product = productService.get(productId);
        if("0".equals(product.getIsPublic()) || "0".equals(product.getOnsale())){
            return msgBean.failure("善品已失效!").returnMsg();
        }
        QueryFilter filter = new DynamicSQLFilterImpl();
        filter.addFilter("Q_uid_L_EQ", String.valueOf(uid));
        filter.addFilter("Q_productId_L_EQ", String.valueOf(productId));
        List<UserCart> cartList = userCartService.find(filter);
        UserCart cart = null;
        if(cartList != null && !cartList.isEmpty()){
            cart = cartList.get(0);
            cart.setProductName(product.getName());
            cart.setImageFile(product.getImageFile());
            cart.setProductCount(cart.getProductCount() + productCount);
            userCartService.update(cart);
        }else {
            cart = new UserCart();
            cart.setUid(uid);
            cart.setProductId(productId);
            cart.setProductName(product.getName());
            cart.setImageFile(product.getImageFile());
            cart.setProductCount(productCount);
            userCartService.insert(cart);
        }
        return msgBean.returnMsg();
    }

    @ApiOperation(value="删除购物车",httpMethod = "POST")
    @RequestMapping("/delete")
    public @ResponseBody  Object deleteProfile(
            @ApiParam("商品ID") @RequestParam(name="id", required = true) Long id,
            @ApiIgnore BaseVo vo,
            @ApiIgnore HttpServletRequest request){
        MsgBean msgBean = new MsgBean();
        Long uid = BaseVo.getUIDFromHeader(request);
        UserCart cart = userCartService.get(id);
        if(cart == null || cart.getUid() != uid){
            return msgBean.failure("联系人不存在!").returnMsg();
        }
        userCartService.remove(id);
        return msgBean.returnMsg();
    }
}
